<template>
  <div class="shopcar-con">
    <van-swipe-cell v-for="item in carList" :key="item.productId">
      <div class="box">
        <van-checkbox @click="checkedCell" v-model="item.checked" checked-color="#F63515"></van-checkbox>
        <img :src="item.imageHost + item.mainImage" @click="toDetail(item.productId, $event)" />
        <div class="item-info" @click="toDetail(item.productId, $event)">
          <p class="item-name">{{ item.name }}</p>
          <p class="item-subtitle">{{ item.subtitle }}</p>
          <div class="option">
            <span class="item-price">￥{{ item.price }}</span>
            <van-stepper @click.stop="addItem" v-model="item.count" min="1" />
          </div>
        </div>
      </div>
      <template slot="right">
        <van-button square @click="delGoods(item)" type="danger" text="删除" />
      </template>
    </van-swipe-cell>
  </div>
</template>

<script>
import { delMyGoods } from '@/http/index.js'
import { mapState } from 'vuex'
export default {
  data() {
    return {
      checked: false,
      value: 1,
    }
  },
  // props: {
  //   car: {
  //     type: Array,
  //     require: true,
  //     default: function () {
  //       return []
  //     },
  //   },
  // },
  computed: {
    ...mapState(['carList']),
  },
  methods: {
    checkedCell() {
      this.$store.dispatch('changeSingleChecked')
    },
    toDetail(id, e) {
      // console.log(id, e)
      if (e.target.className.indexOf('van-stepper') === -1) {
        this.$router.push(`/productDetail/${id}`)
      }
    },
    delGoods(item) {
      // console.log(item)
      this.$dialog
        .confirm({
          title: '提示',
          message: '确认删除吗',
        })
        .then(() => {
          // on close
          delMyGoods({ id: item.id }).then(() => {
            this.$toast('删除成功')
            // 重新获取购物车数据
            this.$nextTick(() => {
              this.$store.dispatch('getCarDataList')
            })
          })
        })
        .catch(() => {
          // on cancel
          this.$toast('已取消删除')
        })
    },
  },
}
</script>

<style lang="scss" scoped>
.shopcar-con {
  margin-top: 100px;
  margin-bottom: 10px;
  padding: 5px;
  background-color: white;
  .box {
    display: flex;
    justify-content: space-around;
    height: 200px;
    img {
      width: 150px;
      height: 150px;
      margin: 0 10px;
    }
    .item-info {
      display: flex;
      flex: 70%;
      flex-direction: column;
      justify-content: space-between;
    }
    .item-name {
      height: 70px;
      font-size: 26px;
      overflow: hidden;
    }
    .item-subtitle {
      font-size: 24px;
      color: #999;
      width: 100%;
      height: 30px;
      overflow: hidden;
    }
    .option {
      display: flex;
      justify-content: space-between;
      align-items: center;
      .item-price {
        font-size: 30px;
        color: red;
      }
    }
  }
  .van-button {
    height: 200px;
    .van-button__content {
      margin-top: -30px;
    }
  }
}
</style>
